/**
 * @file lv_toolbar_demo.c
 *
 */

/*********************
 *      INCLUDES
 *********************/
#include "lvgl.h"
#include "lv_toolbar.h"

/*********************
 *      DEFINES
 *********************/


/**********************
 *      TYPEDEFS
 **********************/

/**********************
 *  STATIC PROTOTYPES
 **********************/


/**********************
 *  STATIC VARIABLES
 **********************/


/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
 **********************/

static void button1_event_handler(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    if(code == LV_EVENT_CLICKED) {
        lv_obj_t * toolbar = (lv_obj_t *)lv_event_get_user_data(e);
        lv_obj_align(toolbar, LV_ALIGN_TOP_LEFT, 8, 8);
        lv_toolbar_set_align(toolbar, LV_TOOLBAR_ALIGN_RIGHT);
        lv_toolbar_update(toolbar);
    }
}

static void button2_event_handler(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    if(code == LV_EVENT_CLICKED) {
        lv_obj_t * toolbar = (lv_obj_t *)lv_event_get_user_data(e);
        lv_obj_set_align(toolbar, LV_ALIGN_TOP_RIGHT);
        lv_obj_align(toolbar, LV_ALIGN_TOP_RIGHT, -8, 8);
        lv_toolbar_set_align(toolbar, LV_TOOLBAR_ALIGN_BOTTOM);
        lv_toolbar_update(toolbar);
    }
}

static void button3_event_handler(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    if(code == LV_EVENT_CLICKED) {
        lv_obj_t * toolbar = (lv_obj_t *)lv_event_get_user_data(e);
        lv_obj_align(toolbar, LV_ALIGN_BOTTOM_RIGHT, -8, -8);
        lv_toolbar_set_align(toolbar, LV_TOOLBAR_ALIGN_LEFT);
        lv_toolbar_update(toolbar);
    }
}

static void button4_event_handler(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    if(code == LV_EVENT_CLICKED) {
        lv_obj_t * toolbar = (lv_obj_t *)lv_event_get_user_data(e);
        lv_obj_align(toolbar, LV_ALIGN_BOTTOM_LEFT, 8, -8);
        lv_toolbar_set_align(toolbar, LV_TOOLBAR_ALIGN_TOP);
        lv_obj_invalidate(toolbar);
    }
}

void lv_toolbar_demo(void)
{
    lv_obj_t * toolbar = lv_toolbar_create(lv_screen_active());
    lv_obj_set_style_radius(toolbar, 10, LV_PART_MAIN);
    lv_obj_set_style_bg_opa(toolbar, LV_OPA_COVER, LV_PART_MAIN);
    lv_obj_set_style_bg_color(toolbar, lv_palette_lighten(LV_PALETTE_GREY, 1), LV_PART_MAIN);
    lv_obj_set_style_outline_width(toolbar, 2, LV_PART_MAIN);
    lv_obj_set_style_outline_color(toolbar, lv_palette_main(LV_PALETTE_BLUE), LV_PART_MAIN);
    lv_obj_set_style_outline_pad(toolbar, 5, LV_PART_MAIN);

    //lv_toolbar_set_start_spacing(toolbar, 10);
    lv_toolbar_set_mid_spacing(toolbar, 5);
    lv_obj_align(toolbar, LV_ALIGN_TOP_LEFT, 8, 8); //default position
    //LED indicates start
    lv_obj_t * led  = lv_led_create(toolbar);
    lv_led_off(led);
    //position to top->left
    lv_obj_t * button = lv_button_create(toolbar);
    lv_obj_set_size(button, 128, 24);
    lv_obj_t  *label = lv_label_create(button);
    lv_label_set_text(label, "top->left");
    lv_obj_set_align(label, LV_ALIGN_CENTER);
    lv_obj_add_event_cb(button, button1_event_handler, LV_EVENT_CLICKED, toolbar);
    //position to top->right
    button = lv_button_create(toolbar);
    lv_obj_set_size(button, 128, 24);
    label = lv_label_create(button);
    lv_label_set_text(label, "top->right");
    lv_obj_set_align(label, LV_ALIGN_CENTER);
    lv_obj_add_event_cb(button, button2_event_handler, LV_EVENT_CLICKED, toolbar);
    //position to bottom->left
    button = lv_button_create(toolbar);
    lv_obj_set_size(button, 128, 24);
    label = lv_label_create(button);
    lv_label_set_text(label, "bottom->right");
    lv_obj_set_align(label, LV_ALIGN_CENTER);
    lv_obj_add_event_cb(button, button3_event_handler, LV_EVENT_CLICKED, toolbar);
    //position to bottom->top
    button = lv_button_create(toolbar);
    lv_obj_set_size(button, 128, 24);
    label = lv_label_create(button);
    lv_label_set_text(label, "bottom->left");
    lv_obj_set_align(label, LV_ALIGN_CENTER);
    lv_obj_add_event_cb(button, button4_event_handler, LV_EVENT_CLICKED, toolbar);

    lv_toolbar_update(toolbar);
}
